package com.freemalll.common.core.web.page;

import com.freemalll.common.core.utils.StringUtils;

/**
 * 分页数据
 * 
 * @author ruoyi
 */
public class PageDomain
{
    /** 当前记录起始索引 */
    private Integer pageNum;

    /** 每页显示记录数 */
    private Integer pageSize;

    /** 排序列 */
    private String orderByColumn;

    /** 排序的方向desc或者asc */
    private String isAsc = "asc";

    /** 分页参数合理化 */
    private Boolean reasonable = true;

    public String getOrderBy()
    {
        if (StringUtils.isEmpty(orderByColumn))
        {
            return "";
        }
        return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc;
    }

    public Integer getPageNum()
    {
        return pageNum;
    }

    public void setPageNum(Integer pageNum)
    {
        this.pageNum = pageNum;
    }

    public Integer getPageSize()
    {
        return pageSize;
    }

    public void setPageSize(Integer pageSize)
    {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn()
    {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn)
    {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc()
    {
        return isAsc;
    }

    public void setIsAsc(String isAsc)
    {
        if (StringUtils.isNotEmpty(isAsc))
        {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc))
            {
                isAsc = "asc";
            }
            else if ("descending".equals(isAsc))
            {
                isAsc = "desc";
            }
            this.isAsc = isAsc;
        }
    }

    public Boolean getReasonable()
    {
        if (StringUtils.isNull(reasonable))
        {
            return Boolean.TRUE;
        }
        return reasonable;
    }

    public void setReasonable(Boolean reasonable)
    {
        this.reasonable = reasonable;
    }

    // 添加 ThreadLocal 支持
    private static final ThreadLocal<PageDomain> PAGE_DOMAIN = new ThreadLocal<>();

    /**
     * 设置分页参数到 ThreadLocal
     */
    public static void setCurrentPageNum(Integer pageNum)
    {
        PageDomain pageDomain = PAGE_DOMAIN.get();
        if (pageDomain == null)
        {
            pageDomain = new PageDomain();
            PAGE_DOMAIN.set(pageDomain);
        }
        pageDomain.setPageNum(pageNum);
    }

    public static void setCurrentPageSize(Integer pageSize)
    {
        PageDomain pageDomain = PAGE_DOMAIN.get();
        if (pageDomain == null)
        {
            pageDomain = new PageDomain();
            PAGE_DOMAIN.set(pageDomain);
        }
        pageDomain.setPageSize(pageSize);
    }

    public static void setCurrentOrderBy(String orderBy)
    {
        PageDomain pageDomain = PAGE_DOMAIN.get();
        if (pageDomain == null)
        {
            pageDomain = new PageDomain();
            PAGE_DOMAIN.set(pageDomain);
        }
        // 解析 orderBy 字符串
        if (StringUtils.isNotEmpty(orderBy))
        {
            String[] parts = orderBy.split(" ");
            if (parts.length >= 1)
            {
                pageDomain.setOrderByColumn(parts[0]);
            }
            if (parts.length >= 2)
            {
                pageDomain.setIsAsc(parts[1]);
            }
        }
    }

    public static void setCurrentReasonable(Boolean reasonable)
    {
        PageDomain pageDomain = PAGE_DOMAIN.get();
        if (pageDomain == null)
        {
            pageDomain = new PageDomain();
            PAGE_DOMAIN.set(pageDomain);
        }
        pageDomain.setReasonable(reasonable);
    }

    /**
     * 获取当前线程的分页参数
     */
    public static PageDomain getCurrentPage()
    {
        return PAGE_DOMAIN.get();
    }

    /**
     * 清理 ThreadLocal
     */
    public static void clearPage()
    {
        PAGE_DOMAIN.remove();
    }
}
